//
//  TextWithBubble.m
//  MiniWeChat
//
//  Created by ByteDance on 2025/6/27.
//

#import "TextWithBubble.h"

@interface TextWithBubble ()

@property(nonatomic, assign) UIEdgeInsets insets;

@end

@implementation TextWithBubble

- (void)setInsets:(UIEdgeInsets)insets {
    _insets = insets;
}

- (void)setBackground:(UIColor *)color {
    self.layer.backgroundColor = color.CGColor;
}

- (void)setTextStr:(NSString *)text {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5.5]; // 行间距
    paragraphStyle.alignment = NSTextAlignmentJustified; // 文本对齐方式 左右对齐（两边对齐）
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])]; // 设置段落样式
    
    self.attributedText = attributedString;
}

#pragma mark - 设置内边距
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds, self.insets) limitedToNumberOfLines:numberOfLines];
    rect.origin.x -= self.insets.left;
    rect.origin.y -= self.insets.top;
    rect.size.width += self.insets.left + self.insets.right;
    rect.size.height += self.insets.top + self.insets.bottom;
    return rect;
}

- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.insets)];
}

@end
